﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverChart.Core.Log;
using System.Collections.Generic;
using SilverChart.Configuration;

namespace SilverChart.Components {
    public class PolarCoordinateAxis : AxisBaseComponent, RelativeClientProvider {
        private static Logger logger = Logger.GetLogger<PolarCoordinateAxis>();

        private Size clientSize;
        private Rect clientRect;

        public PolarCoordinateAxis()
            : base() {
            OriginX = 0;
            OriginY = 0;
        }

        public Double OriginX { get; private set; }
        public Double OriginY { get; private set; }

        public Double MapValueToCanvasLeft(Double distance, Double angle) {
            return distance * Math.Cos(angle) + OriginX;
        }

        public Double MapValueToCanvasTop(Double distance, Double angle) {
            return distance * Math.Sin(angle) + OriginY;
        }

        protected override Size MeasureOverride(Size availableSize) {
            logger.Debug("MeasureOverride:" + availableSize.ToString());
            Size aSize = new Size(Math.Min(availableSize.Width, 10000), Math.Min(availableSize.Height, 10000));
            Double length = Math.Min(aSize.Width, aSize.Height);
            clientSize = new Size(length, length);
            logger.Debug("Measure over:" + aSize.ToString());
            return aSize;
        }

        protected override Size ArrangeOverride(Size finalSize) {
            logger.Debug("ArrangeOverride:" + finalSize.ToString());
            Double centerX = finalSize.Width / 2;
            Double centerY = finalSize.Height / 2;
            Double length = Math.Min(finalSize.Width, finalSize.Height);
            clientRect = new Rect(centerX - length / 2, centerY - length / 2, length, length);
            OriginX = length / 2;
            OriginY = length / 2;
            logger.Debug("Relative Client Rect:" + clientRect.ToString());
            return finalSize;
        }

        public override string WidgetName {
            get {
                return "Polar Coordinate Axis";
            }
        }

        #region RelativeClientProvider Members

        public Rect GetRelativeClientArea(Rect orginalClientRect, RelativeClientConsumer consumer) {
            return clientRect;
        }

        public Size GetRelativeClientSize(Size orginalCilentSize, RelativeClientConsumer consumer) {
            return clientSize;
        }

        #endregion

        public override List<Element> Datas { get { return null; } }
    }
}
